/**
 * @author Administrator
 *
 * @email  zhaozhongyang717@126.com
 *
 * @address 成都天府软件园天空传媒
 *
 * @date  2010-4-18
 */
package com.monitor.action.login;

import com.monitor.action.MonitorActionSupport;
import com.monitor.po.Administrator;
import com.monitor.po.User;
import com.monitor.service.AdminService;
import com.monitor.service.UserService;
import com.monitor.util.CommenUtil;
import com.monitor.util.Constants;
import com.monitor.util.EncoderUtil;

/**
 * @author Administrator
 * 
 */
public class ChangePassswordAction extends MonitorActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3028461160709918189L;
	private String oldpassword;
	private String newpassword;
	private String repassword;
	private AdminService adminService;
	private UserService userService;

	@Override
	public String execute() throws Exception {
		if (CommenUtil.isEmpty(oldpassword) || CommenUtil.isEmpty(newpassword) || CommenUtil.isEmpty(repassword)) {
			if (isAdmin()) {
				return INPUT;
			} else {
				return "userinput";
			}
		}
		if (!newpassword.equals(repassword)) {
			if (isAdmin()) {
				return INPUT;
			} else {
				return "userinput";
			}
		}
		if (isAdmin()) {
			Administrator admin = (Administrator) session.get(Constants.SESSION_USER);
			String rpassword = admin.getPassword();
			if (!EncoderUtil.encode(oldpassword, Constants.ENCODE_MD5).equals(rpassword)) {
				addFieldError("oldpassword", getText("password.error"));
				return INPUT;
			}
			admin.setPassword(EncoderUtil.encode(newpassword, Constants.ENCODE_MD5));
			adminService.changePassword(admin);
			session.put(Constants.SESSION_USER, admin);
			addFieldError("oldpassword", getText("change.ok"));
			return SUCCESS;
		} else {
			User user = (User) session.get(Constants.SESSION_USER);
			String rpassword = user.getPassword();
			if (!rpassword.equals(oldpassword)) {
				addFieldError("oldpassword", getText("password.error"));
				return "userinput";
			}
			user.setPassword(newpassword);
			userService.updateUser(user);
			session.put(Constants.SESSION_USER, user);
			addFieldError("oldpassword", getText("change.ok"));
			return "usersuccess";
		}

	}

	public String changePwd() throws Exception {
		if (isAdmin()) {
			return SUCCESS;
		} else {
			return "usersuccess";
		}
	}

	/**
	 * @return the oldpassword
	 */
	public String getOldpassword() {
		return oldpassword;
	}

	/**
	 * @param oldpassword
	 *            the oldpassword to set
	 */
	public void setOldpassword(String oldpassword) {
		this.oldpassword = oldpassword;
	}

	/**
	 * @return the newpassword
	 */
	public String getNewpassword() {
		return newpassword;
	}

	/**
	 * @param newpassword
	 *            the newpassword to set
	 */
	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}

	/**
	 * @return the repassword
	 */
	public String getRepassword() {
		return repassword;
	}

	/**
	 * @param repassword
	 *            the repassword to set
	 */
	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}

	/**
	 * @return the adminService
	 */
	public AdminService getAdminService() {
		return adminService;
	}

	/**
	 * @param adminService
	 *            the adminService to set
	 */
	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
